package com.xszx.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xszx.serviceedu.entity.Teacher;
import com.xszx.serviceedu.entity.query.TeacherQuery;
import com.xszx.serviceedu.mapper.TeacherMapper;
import com.xszx.serviceedu.service.TeacherService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author lhp
 * @since 2024-07-08
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Override
    public Map<String, Object> pageListWeb(Page<Teacher> pageParam) {

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");

        baseMapper.selectPage(pageParam, queryWrapper);

        List<Teacher> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }
    @Override//模糊查询教师
    public void pageQuery(Page<Teacher> pageParm, TeacherQuery teacherQuery) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sort");//意思是查出来的教师信息按照sort排序
        if(teacherQuery ==null){
            baseMapper.selectPage(pageParm,null);
            return;//结束方法
        }
        if(StringUtils.isNotEmpty(teacherQuery.getName())){//查询如果教师名字不为空
            wrapper.like("name",teacherQuery.getName());

        }
        if(teacherQuery.getLevel() != null){//查询等级相等的
            wrapper.eq("level",teacherQuery.getLevel());
        }
        if(StringUtils.isNotEmpty(teacherQuery.getBegin())){//查询在日期之后的
            wrapper.ge("gmt_create",teacherQuery.getBegin());//大于等于
        }
        if(StringUtils.isNotEmpty(teacherQuery.getEnd())){
            wrapper.le("gmt_create",teacherQuery.getEnd());//小于等于
        }
        baseMapper.selectPage(pageParm,wrapper);//查询
    }

    @Cacheable(value = "teacher",key = "'indexTeacher'")
    @Override
    public List<Teacher> indexTeacher() {
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.orderByDesc("id");
        teacherQueryWrapper.last(" limit 4 ");
        List<Teacher> list = baseMapper.selectList(teacherQueryWrapper);
        return list;
    }

    @Override
    @CacheEvict(value = "teacher",allEntries = true)
    public boolean removeById(Serializable id) {
        Integer i = baseMapper.deleteById(id);
        if(i > 0){
            return true;
        }else{
            return false;
        }
    }
}
